package com.wemei.auto.ui.utils

import android.text.Editable
import android.view.View
import android.widget.CheckBox
import android.widget.CompoundButton
import android.widget.TextView
import androidx.lifecycle.LifecycleOwner
import com.wemei.auto.ui.ext.DefaultLifecycleObserver
import com.wemei.auto.ui.ext.toastMessageLong
import com.wemei.auto.ui.listener.OnValidationResultListener
import com.wemei.auto.ui.listener.TextWatcher

/**
 *校验view值正确性
 */
fun LifecycleOwner.variableView(variable: VariableViewUtils.() -> Unit): VariableViewUtils {
    return VariableViewUtils(this)
        .apply(variable)
        //初始化完毕开始校验一次
        .apply { validator() }
}

class VariableViewUtils(owner: LifecycleOwner) : DefaultLifecycleObserver {
    /**
     * 需要校验的TextView
     */
    private val variableTextView: MutableList<TextView> = mutableListOf()

    /**
     * 需要校验的CheckBox
     */
    private val variableCheckBox: MutableList<CheckBox> = mutableListOf()

    /**
     * 校验结果接收者
     */
    private var mOnValidationResultListener: OnValidationResultListener? = null

    /**
     * 校验过程拦截器
     */
    private var mOnValidationInterceptListener: ((View) -> Boolean?)? = null

    /**
     * 是否提示view的hint值
     */
    private var isShowHint = false

    private val textViewMap: MutableMap<TextView, TextWatcher> = mutableMapOf()

    init {
        owner.lifecycle.addObserver(this)
    }

    /** 验证结果接收者 */
    fun addOnValidationResultListener(onValidationResultListener: OnValidationResultListener? = null) {
        if (onValidationResultListener != null) {
            mOnValidationResultListener = onValidationResultListener
        }
    }

    /** 自定义验证 */
    fun addOnValidationInterceptListener(onValidationInterceptListener: ((View) -> Boolean?)? = null) {
        if (onValidationInterceptListener != null) {
            mOnValidationInterceptListener = onValidationInterceptListener
        }
    }

    fun addTextViews(vararg textViews: TextView?) {
        textViews.forEach { addTextView(it) }
    }

    fun addTextView(textView: TextView?) {
        textView?.let {
            textViewMap[textView] = TextWatcher(textView) { textView: TextView, _: Editable -> validator() }
            textView.addTextChangedListener(textViewMap[textView])
            variableTextView.add(textView)
        }
    }

    fun removeTextView(vararg textViews: TextView?) {
        textViews.forEach {
            if (it != null) {
                it.removeTextChangedListener(textViewMap[it])
                variableTextView.remove(it)
            }
        }
    }

    fun addCheckBox(checkBox: CheckBox?) {
        checkBox?.let {
            checkBox.setOnCheckedChangeListener(this::onCheckedChanged)
            variableCheckBox.add(checkBox)
        }
    }

    private fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {
        validator()
    }

    /** 验证并且设置按钮状态 */
    fun validator() {
        isShowHint = false
        mOnValidationResultListener?.enable(textValidator())
    }

    /** 验证并且提示hint设置的信息*/
    fun clickValidator(): Boolean {
        isShowHint = true
        return textValidator() && checkBoxValidator()
    }

    private fun textValidator(): Boolean {
        for (tv in variableTextView) {
            val isNotEmpty = when (val validationIntercept = mOnValidationInterceptListener?.invoke(tv)) {
                null -> tv.text.isNotEmpty()
                else -> validationIntercept
            }
            if (!isNotEmpty) {
                if (isShowHint) {
                    tv.hint.toString().toastMessageLong()
                }
                return false
            }
        }
        return true
    }

    private fun itemTextValidator(textView: TextView): Boolean? {
        val isNotEmpty = when (val validationIntercept = mOnValidationInterceptListener?.invoke(textView)) {
            null -> textView.text.isNotEmpty()
            else -> validationIntercept
        }
        if (!isNotEmpty) {
            if (isShowHint) {
                textView.hint.toString().toastMessageLong()
            }
            return false
        }
        return null
    }

    private fun checkBoxValidator(): Boolean {
        for (checkBox in variableCheckBox) {
            val isChecked =
                when (val validationIntercept = mOnValidationInterceptListener?.invoke(checkBox)) {
                    null -> checkBox.isChecked
                    else -> validationIntercept
                }
            if (!isChecked) {
                if (isShowHint && checkBox.text.isNotEmpty()) {
                    checkBox.text.toString().toastMessageLong()
                }
                return false
            }
        }
        return true
    }

    override fun onDestroy(owner: LifecycleOwner) {
        variableTextView.clear()
        variableCheckBox.clear()
        owner.lifecycle.removeObserver(this)
    }
}